package ar.com.photo_admin;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class ImageShareCommand {

	public void share(final Context context, String url) {
		ImageLoader.getInstance().loadImage(context, url, new SimpleImageLoadingListener() {
		    
			private ProgressDialog dialog = null;
			
			@Override
		    public void onLoadingComplete(Bitmap loadedImage) {
				String path = Images.Media.insertImage(context.getContentResolver(), 
						loadedImage, "temp_image", null);
				
				Uri screenshotUri = Uri.parse(path);

				Intent sharingIntent = new Intent(Intent.ACTION_SEND);
				sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
				sharingIntent.setType("image/*");

				context.startActivity(Intent.createChooser(sharingIntent, context.getString(R.string.share_photo)));						

				this.dialog.dismiss();
		    }
			
			@Override
			public void onLoadingStarted() {
				super.onLoadingStarted();
				
				dialog = new ProgressDialog(context);
				dialog.setTitle(context.getString(R.string.loading));
				dialog.setMessage(context.getString(R.string.sharing_photo));
				dialog.setIndeterminate(true);
				dialog.setCancelable(false);
				dialog.show();
			}
			
			@Override
			public void onLoadingCancelled() {
				super.onLoadingCancelled();
				this.dialog.dismiss();
			}
			
			@Override
			public void onLoadingFailed(FailReason failReason) {
				super.onLoadingFailed(failReason);
				this.dialog.dismiss();
				Toast.makeText(context, R.string.share_photo_fail, Toast.LENGTH_SHORT).show();
			}
		});
	}
}
